//
//  AppDelegate.m
//  Travel_World
//
//  Created by lanou on 15/10/12.
//  Copyright © 2015年 TLS. All rights reserved.
//

#import "AppDelegate.h"
#import "TLS_WelcomeVC.h"
#import "TLS_FirstPageVC.h"
#import "TLS_DestinationVC.h"
#import "TLS_CommunityPageVC.h"
#import "TLS_MyPageVC.h"
#import "RESideMenu.h"// 抽屉
#import "TLS_LeftVC.h"
#import "TLSSingleton.h"
#import "AFNetworkReachabilityManager.h"


@interface AppDelegate ()
@property (nonatomic, strong) UIView *nigthView;
@end

@implementation AppDelegate
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    //友盟分享设置appkey
    [UMSocialData setAppKey:@"56299019e0f55a05f2004b14"];
    
    //判断网络状态------------------
    AFNetworkReachabilityManager *checkNetwork = [AFNetworkReachabilityManager sharedManager];
    [checkNetwork startMonitoring];// 开始网络检测
    [checkNetwork setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusNotReachable){
            [TLSSingleton shareSingleton].isWIFI = NO;
        }else{
            [TLSSingleton shareSingleton].isWIFI = YES;
        }
    }];
    [checkNetwork stopMonitoring];// 停止网络检测
    
    UINavigationController *firstNAV = [[UINavigationController alloc] initWithRootViewController:[[TLS_FirstPageVC alloc] init]];
    firstNAV.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"推荐" image:[UIImage imageNamed:@"iconfont-home"] tag:101];
    
    UINavigationController *destinationNAV = [[UINavigationController alloc] initWithRootViewController:[[TLS_DestinationVC alloc] init]];
    destinationNAV.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"目的地" image:[UIImage imageNamed:@"iconfont-zuji"] tag:102];
    
    UINavigationController *communityNAV = [[UINavigationController alloc] initWithRootViewController:[[TLS_CommunityPageVC alloc] init]];
    communityNAV.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"社区" image:[UIImage imageNamed:@"iconfont-dianpu"] tag:103];
    
    UINavigationController *myNAV = [[UINavigationController alloc] initWithRootViewController:[[TLS_MyPageVC alloc] init]];
    myNAV.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"iconfont-mine"] tag:104];
    
    NSArray *tabbarArr = @[firstNAV,destinationNAV,communityNAV,myNAV];
    UITabBarController *tabbarVC = [[UITabBarController alloc] init];
    tabbarVC.tabBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_login"]];
    tabbarVC.tabBar.translucent = NO;
    tabbarVC.viewControllers = tabbarArr;

    
    // 初始化抽屉
    TLS_LeftVC *leftVC = [[TLS_LeftVC alloc] init];
    RESideMenu *sideMenuVC = [[RESideMenu alloc] initWithContentViewController:tabbarVC leftMenuViewController:nil rightMenuViewController:leftVC];
    sideMenuVC.backgroundImage = [UIImage imageNamed:@"meetYou.jpg"];
    sideMenuVC.menuPreferredStatusBarStyle = 1;
    sideMenuVC.contentViewShadowColor = [UIColor blackColor];
    sideMenuVC.contentViewShadowOffset = CGSizeMake(10, 10);
    sideMenuVC.contentViewShadowOpacity = 0.6;
    sideMenuVC.contentViewShadowRadius = 12;
    sideMenuVC.contentViewShadowEnabled = YES;
 
    
    TLS_WelcomeVC *welcomeVC = [[TLS_WelcomeVC alloc] init];
    void(^block)() = ^(){
        [welcomeVC removeFromParentViewController];
        self.window.rootViewController = sideMenuVC;
    };
    welcomeVC.block = block;
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    if ([userDefault objectForKey:@"iSFirstLogin"] == nil) {
        [userDefault setObject:@"NO" forKey:@"iSFirstLogin"];
        //[TLSSingleton shareSingleton].isNight = NO;
        self.window.rootViewController = welcomeVC;
    } else {
        self.window.rootViewController = sideMenuVC;
    }
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [TLSSingleton shareSingleton].isNight = [[defaults objectForKey:@"isNight"] boolValue];
    self.nigthView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.nigthView.userInteractionEnabled = NO;
    self.nigthView.backgroundColor = [UIColor blackColor];
    [self.window addSubview:self.nigthView];
    [self isNight];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isNight) name:@"isNight" object:nil];
    
    return YES;
}
- (void)isNight
{
    if ([TLSSingleton shareSingleton].isNight)
    {  [self.window addSubview:self.nigthView];
        self.nigthView.alpha = 0.5;
        NSLog(@"黑夜");
    } else
    {
        [self.window addSubview:self.nigthView];
        self.nigthView.alpha = 0;
        NSLog(@"白天");
    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
